/*
 * Config.h
 *
 *  Created on: 05/06/2011
 *      Author: George Ferreira
 */

#ifndef CONFIG_H_
#define CONFIG_H_
#include <fstream>
#include <iostream>
#include "input.h"
#include "defines.h"

using namespace std;

class Config {
	public:
		int sourceType;
		int timeToWait;
		int promiscuous;
		int snapeLen;

		string source;
		string filter;
		string *configfile;

	private:
		ifstream *config;
		Input *userInput;

	public:
		Config(string *configfile, Input *userInput){
			this->configfile = configfile;
			this->config = new ifstream(configfile->c_str());
			this->userInput = userInput;

			if( !config->is_open() ){
				cout << "O arquivo de configuração " << configfile << "não pode ser aberto" << endl;
				exit(1);
			}

			this->parseFileConfig();
			this->parseUserInputParams();
		}

		int parseFileConfig(){
			string param;
			string value;

			while ( this->config->good() ){
				getline(*config, param, '=');
				getline(*config, value);

				if ( param == "sourceType"){

					if ( value == "interface" ){
						this->sourceType = INTERFACE;
					}else{
						this->sourceType = FILESOURCE;
					}

				}else if( param == "timeToWait" ){

					this->timeToWait = atoi(value.c_str());

				}else if( param == "promiscuous" ){

					this->promiscuous = atoi(value.c_str());

				}else if( param == "snapeLen" ){

					this->snapeLen = atoi(value.c_str());

				}else if ( param == "filter" ){

					this->filter = value;

				}else if ( param == "source" ){

					this->source = value;

				}
			}

			this->config->close();

			delete this->config;

			return 1;
		}

		void parseUserInputParams(){

			if ( this->userInput->sourcename == INTERFACE ){
				this->sourceType  = INTERFACE;
				this->source = this->userInput->interface;
			}else if ( this->userInput->sourcename == FILESOURCE ){
				this->sourceType = FILESOURCE;
				this->source = this->userInput->sourcefile;
			}
		}

		void showParams(){
			cout << "sourceType: "  <<  this->sourceType << endl;
			cout << "timeToWait: " << this->timeToWait << endl;
			cout << "source: " << this->source << endl;
			cout << "promiscuous: " << this->promiscuous << endl;
			cout << "snapeLen: " << this->snapeLen << endl;
			cout << "filter: " << this->filter << endl;
		}

};


#endif /* CONFIG_H_ */
